package com.base.library;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import com.afollestad.materialdialogs.MaterialDialog;
import com.base.library.manager.DialogManager;
import com.base.library.manager.RealmManager;
import com.base.library.manager.SnackBarManager;

import io.realm.Realm;

public abstract class BaseActivity extends AppCompatActivity {
    public Context mContext;
    private Fragment mContent;
    public FragmentManager fragmentManager;
    private ViewDataBinding viewDataBinding;
    public RealmManager realm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewDataBinding = DataBindingUtil.setContentView(this, getLayoutId());
        mContext = this;
        fragmentManager = getSupportFragmentManager();
        realm = new  RealmManager(Realm.getDefaultInstance());
        initData(savedInstanceState);
    }

    public <T extends ViewDataBinding> T getViewDataBinding() {
        return (T) viewDataBinding;
    }

    public synchronized void init(Fragment from, int layout_id) {
        if (fragmentManager == null) {
            fragmentManager = getSupportFragmentManager();
        }
        try {
            if (from == null) {
                return;
//                from = new NearbyFragment();
            }
            if (!from.isAdded()) {
                mContent = from;
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                if (transaction != null) {
                    transaction.add(layout_id, from, from.getClass().getName()).commitAllowingStateLoss();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void switchContent(Fragment to, int layout_id) {
        if (fragmentManager == null) {
            fragmentManager = getSupportFragmentManager();
        }
        try {
            if (mContent != to) {
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                if (transaction != null) {
                    if (!to.isAdded() && fragmentManager.findFragmentByTag(to.getClass().getName()) == null) { // 先判断是否被add过
                        if (mContent != null) {
                            transaction.hide(mContent);
                        }
                        transaction.add(layout_id, to, to.getClass().getName()).commitAllowingStateLoss(); // 隐藏当前的fragment，add下一个到Activity中
                    } else {
                        if (mContent != null) {
                            transaction.hide(mContent);
                        }
                        transaction.show(to).commitAllowingStateLoss(); // 隐藏当前的fragment，显示下一个
                    }
                    mContent = to;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showMsg(final String msg) {
        runOnUiThread(() -> SnackBarManager.showMessage(viewDataBinding.getRoot(), msg));
    }

    public void showMsg(@StringRes final int resId) {
        runOnUiThread(() -> SnackBarManager.showMessage(viewDataBinding.getRoot(), resId));
    }

    public MaterialDialog showDefaultProgress() {
        return DialogManager.showDefaultProgress(mContext);
    }

    @Override
    protected void onDestroy() {
        realm.close();
        super.onDestroy();
    }

    public abstract int getLayoutId();

    public abstract void initData(Bundle savedInstanceState);
}
